<?php
/**
 * Created by PhpStorm.
 * Author: 黑瞎子
 * Date: 2018/3/17
 * Time: 9:57
 */

namespace app\web\controller\v1;


use app\api\controller\v1\Banner as BannerDate;
use app\api\controller\v1\Theme as ThemeDate;
use app\api\controller\v1\Product as ProductDate;
use app\api\model\Product;
use think\Cache;

class AllDate
{
    /*
     * 测试 一个接口获取首页所有数据，只需请求一次
     * @url /web/v1/all?id=1&ids=1,2,3
     * */
    public function getAllDate($id,$ids = ''){
        $banner = new BannerDate();
        $banner = $banner->getBanner($id);
        $theme = new ThemeDate();
        $theme = $theme->getSimpleList($ids);
        $product = new ProductDate();
        $product = $product->getRecent();

        $allDate = [
            'banner' => $banner,
            'theme' => $theme,
            'product' => $product
        ];
        return $allDate;
    }

    // 获取缓存测试
    public function getCache(){
        $key = 'token';
        $value = [
            'value1' => '1',
            'value2' => '2',
            'value3' => '3',
        ];
        $result = Cache::set($key, $value, 3600);
        $getResult = Cache::get($key);
        return [
            '存入缓存返回的结果' => $result,
            '获取缓存的结果' => $getResult
        ];
    }

    /*
     * 面试的题目
     * 1.数组 a = [1,2,3,4,3,5]，n = 1 ， 返回数组中次数大于n的数据结构。列:[1,3]
     * 2.输入下划线命名的字符串，返回驼峰命名
     * 3.输入正整数，用*打印正三角形
     * */
    public function getInterview(){
        $a = [1,2,3,4,3,5,4];
        $n = 1;
        $string = 'get_product_by_id';
        $result = $this->checkNo($a, $n);
        $newStr = $this->getHumpName($string);
        $this->dis(5);
        return [
            'checkNo' => $result,
            'newStr' => $newStr
        ];
    }

    //返回数组中次数大于n的数据结构
    public function checkNo($a, $n){
        $result = array_count_values($a);
        $newArr = [$n];
        foreach ($result as $key => $value) {
            if ($value > $n){
                array_push($newArr, $key);
            }
        }

        return $newArr;
    }

    //输入下划线命名的字符串，返回驼峰命名
    public function getHumpName($str){
        $str = ucwords(str_replace('_', ' ', $str));
        $str = str_replace(' ','',lcfirst($str));
        return $str;
    }

    // 用*打印三角形
    public function dis($num){
        if($num%2==0){
            $num+=1;
        }

        for( $i=1;$i<=$num;$i++){
            for ($j =$num - 1; $j > $i; $j--){
                echo '&nbsp;&nbsp;';
            }
            for ($k = 1; $k <= $i; $k ++){
                echo '* &nbsp;';
            }
            echo '<br />';
        }
    }
}